package cn.jwinni.springboot.controller;

import cn.jwinni.springboot.common.HttpRequest;
import cn.jwinni.springboot.common.Https;
import cn.jwinni.springboot.common.JsonResult;
import cn.jwinni.springboot.domain.Prodect;
import cn.jwinni.springboot.domain.ValidateCode;
import cn.jwinni.springboot.service.ProdectService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class WxController {
    public static Logger logs = Logger.getLogger(WxController.class);

    @Autowired
    private ProdectService prodectService;

    /**
     * 短信请求地址url
     */
    @Value("${smsUrl}")
    private String smsUrl;
    /**
     * 短信id
     */
    @Value("${corpId}")
    private String corpId;
    /**
     * 短息账号
     */
    @Value("${LoginName}")
    private String LoginName;
    /**
     * 短信密码
     */
    @Value("${SecretKey}")
    private String SecretKey;

    /**
     * 微信报名页面
     *
     * @return
     */
    @RequestMapping("/wx")
    public void wxSubUser(HttpServletResponse response) throws Exception {

        response.sendRedirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx40464ca9897a0f51&redirect_uri=http://www.jwinni.cn/wxcode&response_type=code&scope=snsapi_base#wechat_redirect");

    }

    @RequestMapping("/wxcode")
    public ModelAndView wxSubUsercode(HttpServletRequest request) {
        ModelAndView mav = new ModelAndView();
        String code = request.getParameter("code");
        //页面获取openId接口
        String getopenid_url = "https://api.weixin.qq.com/sns/oauth2/access_token";
        String param = "appid=wx40464ca9897a0f51&secret=64509626107e06bd7893154fd8da6ec5&code=" + code + "&grant_type=authorization_code";
        //向微信服务器发送get请求获取openIdStr
        String openIdStr = HttpRequest.sendGet(getopenid_url, param);
        JSONObject json = JSONObject.parseObject(openIdStr);//转成Json格式
        String openId = json.getString("openid");//获取openId
        mav.addObject("openId", openId);
        System.out.println("openId:" +openId);
        mav.setViewName("/wx/wxSubUser");
        return mav;
    }

    /**
     * 查询有效的项目
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/wxAffectiveProject")
    public JsonResult wxAffectiveProject() {
        try {
            List<Prodect> prodects = prodectService.getAffectiveProject();
            return new JsonResult(prodects);
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:wxAffectiveProject，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }
    }

    /**
     * 校验短信验证码
     *
     * @param code
     * @param telNo
     * @return
     */
    @ResponseBody
    @RequestMapping("/checkVCode")
    public JsonResult checkValidateCode(String code, String telNo) {

        try {
            Integer ret = prodectService.checkVCode(code, telNo);
            if (ret > 0) {
                return new JsonResult();
            } else {

                return new JsonResult("验证码输入错误或失效");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:checkValidateCode，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }

    }

    /**
     * 通过手机号获取验证码（短信验证码发送）
     *
     * @param Tel
     * @return
     */
    @ResponseBody
    @RequestMapping("/getVCode")
    public JsonResult setValidateCode(String Tel) {

        try {
            ValidateCode validateCode = new ValidateCode();
            String VCode = String
                    .valueOf(new Random().nextInt(899999) + 100000);//生成短信验证码
            // 设置验证码失效时间为10分钟
            Calendar cDate = Calendar.getInstance();
            cDate.add(Calendar.MINUTE, 10);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            validateCode.setCode(VCode);
            validateCode.setTelNo(Tel);
            Date a = cDate.getTime();
            validateCode.setExpireDate(sdf.format(cDate.getTime()));
            Long ret = prodectService.insertVCode(validateCode);
            if (ret > 0) {

                String content = "您的验证码为:" + VCode + "，该码有效期为10分钟且只能使用一次！";
                //保存成功，发送短信验证码

                String jsonRet = Https.posts(smsUrl, "SmsGetBalance", corpId, LoginName, SecretKey, Tel, content, "");
                JSONObject jsonObject = JSON.parseObject(jsonRet);
                int Code = Integer.parseInt(jsonObject.getString("Code"));
                if (Code > 0) {
                    //有余额，发短信。
                    Https.posts(smsUrl, "SmsSend", corpId, LoginName, SecretKey, Tel, content, "");
                    return new JsonResult();
                } else {

                    return new JsonResult("没短信余额喽，请充值短信。");
                }
            } else {
                return new JsonResult("验证码发送失败！");

            }
        } catch (Exception e) {
            e.printStackTrace();
            logs.error("function:setValidateCode，msg=" +e.getMessage());
            return new JsonResult("系统错误");
        }
    }


}
